<template>
  <div class="xtx-pay-page">
    <div class="container">
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/cart">购物车</XtxBreadItem>
        <XtxBreadItem>支付订单</XtxBreadItem>
      </XtxBread>
      <!-- 付款信息 -->
      <div class="pay-info">
        <span class="icon iconfont icon-queren2"></span>
        <div class="tip">
          <p>订单提交成功！请尽快完成支付。</p>
          <p v-if="sCount < 0">支付超时</p>
          <p v-else>支付还剩 <span>{{ payObj.countMS }}</span>, 超时后将取消订单</p>
        </div>
        <div class="amount">
          <span>应付总额：</span>
          <span>¥{{ payObj.payMoney }}</span>
        </div>
      </div>
      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">选择以下支付方式付款</p>
        <div class="item">
          <p>支付平台</p>
          <a class="btn wx" href="javascript:;"></a>
          <a class="btn alipay" :href="url"></a>
        </div>
        <div class="item">
          <p>支付方式</p>
          <a class="btn" href="javascript:;">招商银行</a>
          <a class="btn" href="javascript:;">工商银行</a>
          <a class="btn" href="javascript:;">建设银行</a>
          <a class="btn" href="javascript:;">农业银行</a>
          <a class="btn" href="javascript:;">交通银行</a>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getPayInfoAPI } from '@/api'
import { onMounted, ref, onUnmounted, computed } from 'vue'
import { useRoute } from 'vue-router'
import dayjs from 'dayjs'
import { useIntervalFn } from '@vueuse/core'
import { baseURL } from '@/utils/request'
export default {
  name: 'XtxPayPage',
  setup () {
    const route = useRoute()
    const payObj = ref({}) // 支付页面数据对象
    const sCount = ref(0) // 剩余时间
    // 把秒->分和秒
    // TODO: 自己用js实现一次(思路/60向下取整是分钟数, %60取余是秒数, 拼接字符串)
    const formatTime = (time) => {
      return dayjs.unix(time).format('mm分ss秒')
    }
    onMounted(async () => {
      const res = await getPayInfoAPI(route.query.id)
      payObj.value = res.result
      sCount.value = res.result.countdown // 剩余支付时间(秒)
      payObj.value.countMS = formatTime(sCount.value) // 把剩余秒数传入, 返回分和秒
      resume()
    })

    const { pause, resume } = useIntervalFn(() => {
      if (sCount.value === 0) {
        return pause()
      }
      if (sCount.value > 0) {
        sCount.value--
        payObj.value.countMS = formatTime(sCount.value) // 把剩余秒数传入, 返回分和秒
      }
    }, 1000)

    onUnmounted(() => pause())

    // 拼接支付宝跳转后台支付链接上 (a标签href地址)
    const url = computed(() => {
      // 经验: 线下开发环境写localhost加端口号
      // 等你上线,把这个地址换成线上服务器地址
      // 建议: 用环境变量来动态设置(参考人资项目)process.env.VUE_APP_xx
      // 因为#特殊字符, 需要URL编码后再回跳时才能正确解析
      const redirectUrl = encodeURIComponent(
        'http://localhost:8080/#/user/pay/result'
      )
      return `${baseURL}/pay/aliPay?orderId=${route.query.id}&redirect=${redirectUrl}`
    })

    return {
      payObj,
      sCount,
      url
    }
  }
}
</script>
<style scoped lang="less">
.pay-info {
  background: #fff;
  display: flex;
  align-items: center;
  height: 240px;
  padding: 0 80px;
  .icon {
    font-size: 80px;
    color: #1dc779;
  }
  .tip {
    padding-left: 10px;
    flex: 1;
    p {
      &:first-child {
        font-size: 20px;
        margin-bottom: 5px;
      }
      &:last-child {
        color: #999;
        font-size: 16px;
      }
    }
  }
  .amount {
    span {
      &:first-child {
        font-size: 16px;
        color: #999;
      }
      &:last-child {
        color: var(--price-color);
        font-size: 20px;
      }
    }
  }
}
.pay-type {
  margin-top: 20px;
  background-color: #fff;
  padding-bottom: 70px;
  p {
    line-height: 70px;
    height: 70px;
    padding-left: 30px;
    font-size: 16px;
    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }
  .btn {
    width: 150px;
    height: 50px;
    border: 1px solid #e4e4e4;
    text-align: center;
    line-height: 48px;
    margin-left: 30px;
    color: #666666;
    display: inline-block;
    &.active,
    &:hover {
      border-color: var(--xtx-color);
    }
    &.alipay {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png)
        no-repeat center / contain;
    }
    &.wx {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg)
        no-repeat center / contain;
    }
  }
}
</style>
